class Partner < ActiveRecord::Base
  acts_as_paranoid unless SHOW_DELETED
  #belongs_to :tipo_iva
  belongs_to :metodo_pagamento
  belongs_to :partner_collegato, :class_name => 'Partner'
  has_many :partners_collegati, :dependent => :protect, :class_name => 'Partner', :foreign_key => 'partner_collegato_id'
  has_many :indirizzi_aggiuntivi, :as => :indirizzo_aggiuntivo_ref, :dependent => :protect, :class_name => 'IndirizzoAggiuntivo'
  has_many :coordinate_bancarie, :as => :coordinate_bancarie_ref, :dependent => :protect
  has_many :fatture_cliente, :dependent => :protect, :class_name => 'FatturaCliente'
  has_many :fatture_fornitore, :dependent => :protect, :class_name => 'FatturaFornitore'
  has_many :preventivi, :dependent => :protect
  has_many :lettere, :dependent => :protect
  has_and_belongs_to_many :tipi_partner, :join_table => 'tipi_partner_partners', :uniq => true, :class_name => 'TipoPartner'
  has_many :contatti, :as => :contatto_ref, :dependent => :protect
  has_many :allegati, :as => :allegato_ref, :dependent => :destroy
  has_many :note, :as => :nota_ref, :dependent => :protect

  validates_presence_of :ragione_sociale, :tipi_partner
  validates_uniqueness_of :ragione_sociale, :scope => [:ragione_sociale, :deleted_at], :case_sensitive => false
  validates_uniqueness_of :partita_iva, :allow_nil => true
  validates_uniqueness_of :codice_fiscale, :allow_nil => true
  validates_numericality_of :cap, :partita_iva, :allow_nil => true
  validates_length_of :partita_iva, :is => 11, :allow_nil => true
  validates_length_of :codice_fiscale, :is => 16, :allow_nil => true
  validates_format_of :codice_fiscale, :allow_nil => true,
    :with => /^([a-zA-Z]{6})([0-9]{2})([a-zA-Z]{1})([0-9]{2})([a-zA-Z]{1})([0-9]{3})([a-zA-Z]{1})$/

  def name
    ragione_sociale
  end
end
